<?php


namespace app\admin\controller;


use app\common\model\CompLabelModel;
use app\common\model\HelpModel;
use think\facade\Request;
use think\Validate;

class ComplabelController extends BaseController
{

    public function index()
    {
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();

    }

    public function edit()
    {
        $id = Request::param('id', 0, 'intval');
        if ($id < 1) {
            return $this->error('未查询到相关数据');
        }
        $complabel_info = CompLabelModel::get($id);

        $this->assign('complabel_info', $complabel_info);
        return $this->fetch();
    }

    public function getlist()
    {
        $sport_id = Request::param("sport_id");
        $comp     = Request::param("comp");
        $page     = Request::param("page");
        $limit    = Request::param("limit");

        $where = [];
        if (is_numeric($sport_id)) {
            $where[] = ['sport_id', '=', $sport_id];
        }
        if ($comp) {
            $where[] = ['comp', 'like', "%{$comp}%"];
        }

        $userlist = CompLabelModel::where($where)->page($page, $limit)->order("id", "desc")->select();

        if (count($userlist) > 0) {
            $count = CompLabelModel::where($where)->count();
            return json(["code" => 0, "msg" => "", "data" => $userlist, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function edit_post()
    {
        $id        = Request::param('id', 0, 'intval');
        $all_param = Request::param();

        $validate = new Validate([
            'label'     => 'require|length:2,32',
            'comp'      => 'require|length:2,32',
            'sport_id'  => 'require|number',
            'serial'    => 'require|number',
            'label_pic' => 'url',
        ], [
            "label"    => "标签名称在2-32个字符",
            "comp"     => "联赛名称在2-32个字符",
            "sport_id" => "请正确选择联赛类型",
            "serial"   => "排序只能输入整数",
            "icon.url"     => "请正确上传联赛图片",
        ]);

        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }

        $complabel_model = CompLabelModel::get($id);
        if (empty($complabel_model)) {
            return self::bulidFail('联赛标签信息不存在');
        }

        // 查询标题是否已经存在
        $e_where     = [
            ['id', '<>', $id],
            ['label', '=', $all_param['label']],
            ['comp', '=', $all_param['comp']]
        ];
        $exists = CompLabelModel::where($e_where)->field('id')->find();
        if (!empty($exists)) {
            return self::bulidFail('标题已经存在');
        }

        if ($complabel_model->save($all_param)) {
            // 记录日志
            $log_content = "添加联赛标签，ID：{$complabel_model->id}，标签名称：{$complabel_model->label},联赛名称：{$complabel_model->comp}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function add_post()
    {
        $all_param = Request::param();

        $validate = new Validate([
            'label'     => 'require|length:2,32',
            'comp'      => 'require|length:2,32',
            'sport_id'  => 'require|number',
            'serial'    => 'require|number',
            'label_pic' => 'url',
        ], [
            "label"    => "标签名称在2-32个字符",
            "comp"     => "联赛名称在2-32个字符",
            "sport_id" => "请正确选择联赛类型",
            "serial"   => "排序只能输入整数",
            "icon.url"     => "请正确上传联赛图片",
        ]);

        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }

       $complabel_model = new CompLabelModel($all_param);

        // 联赛标签和联赛名称是否存在
        $label_w = [
            ['label','=',$complabel_model->label],
            ['comp','=',$complabel_model->comp]
        ];
        $exists = CompLabelModel::where($label_w)->find();
        if (!empty($exists)) {
            return self::bulidFail('标签名称和联赛名称已存在');
        }

        if ($complabel_model->save()) {
            // 记录日志
            $log_content = "添加联赛标签，ID：{$complabel_model->id}，标签名称：{$complabel_model->label},联赛名称：{$complabel_model->comp}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }


}
